# ScalableCairoLabel - This texture uses cairo to render text
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import cairo
import clutter
from clutter.cluttercairo import CairoTexture

class ScalableCairoLabel(CairoTexture):
    """
    ScalableCairoLabel class

    This class provides a way to render text using cairo. This is a textlabel
    similar to clutter.Label. The difference is that this class uses Cairo for
    rendering.
    """

    def __init__(self, text=""):
        """
        Initialize ArrowTexture
        @param text: Text of the label (example: 'Sans Bold 24')
        """
        CairoTexture.__init__(self, 600, 600)
        
        self.text = text
        self.color = clutter.Color(255,255,255,255)
        self.font_name = "Sans 12"
        self._render()
        
    def set_text(self, text):
        """
        Set text to label. This is the text we render.
        @param text: Text as a string
        """
        self.text = text
        self._render()
        
    def get_text(self):
        """
        Get text of the label.
        @return: Text as a string
        """
        return self.text
    
    def set_color(self, color):
        """
        Set label text color.
        @param color: clutter.Color
        """
        self.color = color
        self._render()
        
    def get_color(self):
        """
        Get current color of the text
        @return: clutter.Color
        """
        return self.color
    
    def get_font_name(self):
        """
        Get font name of this label
        @return: Font name string
        """
        return self.font_name
        
    def set_font_name(self, font_name):
        """
        Set font name for this label. Example: 'Sans Bold 24'
        @param font_name: Fontname string
        """
        self.font_name = font_name
        self._render()
    
    def _render(self):
        """
        Cairo drawing is done here.
        """
        self.context = self.cairo_create()
        #self. context.scale(size, size)
        
        # Set text color
        r = float(self.color.red)   / 255.0
        g = float(self.color.green) / 255.0
        b = float(self.color.blue)  / 255.0
        a = float(self.color.alpha) / 255.0
        self.context.set_source_rgba(r, g, b, a)
               
        self.context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
        self.context.set_font_size(32)
        x, y, w, h = self.context.text_extents(self.text)[:4]

        self.context.move_to(0.5 - w / 2 - x, 0.5 - h / 2 - y)
        self.context.show_text(self.text)
        
        #del(self.context) # Updates texture
        